package com.qf.ems.controller;

import com.qf.ems.entity.EmpManager;
import com.qf.ems.service.EmpManagerService;
import com.qf.ems.service.impl.EmpManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/manager/empManagerLogin")
public class EmpManagerLoginController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取参
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String inputVcode = req.getParameter("inputVcode");
        //验证码
        String codes = (String) req.getSession().getAttribute("codes");
        if(!inputVcode.isEmpty() && inputVcode.equalsIgnoreCase(codes) ){//验证码正确
            //用户名和密码
            EmpManagerService empManagerService = new EmpManagerServiceImpl();
            EmpManager empManager = empManagerService.login(username, password);
            if (empManager != null) {
                //登录成功把用户放入session
                HttpSession session = req.getSession();
                session.setAttribute("empManager",empManager);
                //分治，单一职责
                resp.sendRedirect(req.getContextPath()+"/manager/safe/showAllEmp");
            }else {
                //登录失败到登录页
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }
        }else{//验证码错误
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
        }


    }
}
